//
//  XrLunBoTu.m
//  TasteEnjoy
//
//  Created by XuRui on 16/5/3.
//  Copyright © 2016年 XuRui. All rights reserved.
//


/**
 * 轮播图: 建50个分区 每个item里都是照片数组
 *        无限循环分区里的item
 */

#import "XrLunBoTu.h"

@interface XrLunBoTu ()<UICollectionViewDelegate, UICollectionViewDataSource>

@property (nonatomic, retain) NSArray *imgArray;
@property (nonatomic, retain) BaseCollectionView *collectionView;
@property (nonatomic, retain) XrLunBoTuLayout *layout;
@property (nonatomic, retain) UIPageControl *pageControl;
@property (nonatomic, retain) NSTimer *timer;

@end

@implementation XrLunBoTu


#pragma mark - ------------ 内存管理 ---------------
- (void)dealloc
{
    [_imgArray release];
    [_collectionView release];
    [_layout release];
    [_pageControl release];
    [_timer release];
    _selectBlock = nil;
    [super dealloc];
}

#pragma mark - ------------ 初始化 ---------------

- (instancetype)initWithFrame:(CGRect)frame imgArr:(NSArray *)imgArray selectImageBlock:(void (^)(NSInteger))selectImageBlock
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.selectBlock = selectImageBlock;
        self.imgArray = imgArray;
        [self creatCollectionView];
      // if (imgArray.count != 0) {
            
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:50 / 2 ] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        
    //    }
        [self creatPageControl];
        
        [self addTimer];
    }
    return self;

}

#pragma mark - ------------ 创建collection ---------------
- (void)creatCollectionView
{
    
    self.layout = [[XrLunBoTuLayout alloc]init];
    self.collectionView = [[BaseCollectionView alloc]initWithFrame:self.bounds collectionViewLayout:_layout];
    [self addSubview:_collectionView];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.pagingEnabled = YES;
    [_collectionView registerClass:[XrLunBoCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    [_layout release];
    [_collectionView release];
    
}

#pragma mark - ------------ 返回分区数 ---------------
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{

    return 50;
}

#pragma mark - ------------ 返回每个分区里的item ---------------
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

    return _imgArray.count;
}

#pragma mark - ------------ 赋值 ---------------

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    XrLunBoCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    
    [cell.photo sd_setImageWithURL:[NSURL URLWithString:_imgArray[indexPath.item]] placeholderImage:PLACEHOLDER];
    
    return cell;

    
}

#pragma mark - ------------ collection点击 ---------------
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    self.selectBlock(indexPath.item);
    
}


#pragma mark - ------------ 创建page ---------------
- (void)creatPageControl
{
    self.pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(0, 180, SCREEN_WIDTH, 20)];
    _pageControl.numberOfPages = _imgArray.count;
    _pageControl.currentPage = 0;
//    _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
//    _pageControl.pageIndicatorTintColor = [UIColor yellowColor];
    
    [self addSubview:_pageControl];
    
}

#pragma mark - ------------ 加定时器 ---------------
- (void)addTimer
{
    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(roll) userInfo:nil repeats:YES];
    
    [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
    
}

#pragma mark - ------------ 计时器方法 无限滚动分区里的item ---------------
- (void)roll
{


    NSIndexPath *path = [[_collectionView indexPathsForVisibleItems]lastObject];
    NSIndexPath *currentPath = [NSIndexPath indexPathForItem:path.item inSection:50 / 2];
    
    [_collectionView scrollToItemAtIndexPath:currentPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
    
    
    NSInteger nextItem = currentPath.item + 1;
    NSInteger nextSection = currentPath.section;
    if (nextItem == _imgArray.count) {
        
        nextItem = 0;
        nextSection ++;
        
    }
    
    NSIndexPath *nextIndexPath = [NSIndexPath indexPathForItem:nextItem inSection:nextSection];
    
    [_collectionView scrollToItemAtIndexPath:nextIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:YES];
    

}



#pragma mark - ------------ 点跟着动 ---------------
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{

    self.pageControl.currentPage = (NSInteger)(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5 ) % _imgArray.count;
    
}


#pragma mark - ------------ 即将开始拖拽 计时器停止 ---------------
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{


    [self.timer invalidate];
    self.timer = nil;

}

#pragma mark - ------------ 已经拖拽完减速 计时器在开始 ---------------
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    [self addTimer];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
